package flc.ast.fragment;

import android.graphics.Outline;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.youth.banner.listener.OnPageChangeListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.StrategyActivity;
import flc.ast.adapter.HomeStrategyAdapter;
import flc.ast.adapter.IndicatorAdapter;
import flc.ast.adapter.MyBannerAdapter;
import flc.ast.bean.MyBannerBean;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.DisplayUtil;
import stark.common.api.StkApi;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private IndicatorAdapter indicatorAdapter;
    private HomeStrategyAdapter homeStrategyAdapter;
    private List<MyBannerBean> listBanner = new ArrayList<>();



    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        mDataBinding.ivGame1.setOnClickListener(this);
        mDataBinding.ivGame2.setOnClickListener(this);
        mDataBinding.ivGame3.setOnClickListener(this);
        mDataBinding.ivGame4.setOnClickListener(this);
        mDataBinding.ivGame5.setOnClickListener(this);
        listBanner.add(new MyBannerBean(R.drawable.adanzaipdds, getString(R.string.game_name1), getString(R.string.game_date1)));
        listBanner.add(new MyBannerBean(R.drawable.adafuwbanner, getString(R.string.game_name2), getString(R.string.game_date2)));
        listBanner.add(new MyBannerBean(R.drawable.ayundingzhiybanner, getString(R.string.game_name3), getString(R.string.game_date3)));
        listBanner.add(new MyBannerBean(R.drawable.ahuncf, getString(R.string.game_name4), getString(R.string.game_date4)));
        listBanner.add(new MyBannerBean(R.drawable.axchangpd, getString(R.string.game_name4), getString(R.string.game_date4)));
        MyBannerAdapter bannerAdapter = new MyBannerAdapter(listBanner, mContext);
        //设置切换时为圆角
        mDataBinding.homeBanner.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0,0,view.getWidth(),view.getHeight(), DisplayUtil.dip2px(20,mContext));
            }
        });
        mDataBinding.homeBanner.setClipToOutline(true);
        mDataBinding.homeBanner.setAdapter(bannerAdapter, true);//false不无限循环
        mDataBinding.homeBanner.isAutoLoop(true);//false不自动轮播
        mDataBinding.homeBanner.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                indicatorAdapter.setSelPosition(position);
                indicatorAdapter.notifyDataSetChanged();
                setData(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mDataBinding.rvIndicatorList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        indicatorAdapter = new IndicatorAdapter();
        indicatorAdapter.setSelPosition(0);
        indicatorAdapter.setList(listBanner);
        mDataBinding.rvIndicatorList.setAdapter(indicatorAdapter);
        mDataBinding.rvHomeStrategyList.setLayoutManager(new GridLayoutManager(mContext,2));
        homeStrategyAdapter=new HomeStrategyAdapter();
        mDataBinding.rvHomeStrategyList.setAdapter(homeStrategyAdapter);
        homeStrategyAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivGame1:
                StrategyActivity.type=0;
                startActivity(StrategyActivity.class);
                break;
            case R.id.ivGame2:
                StrategyActivity.type=1;
                startActivity(StrategyActivity.class);
                break;
            case R.id.ivGame3:
                StrategyActivity.type=2;
                startActivity(StrategyActivity.class);
                break;
            case R.id.ivGame4:
                StrategyActivity.type=3;
                startActivity(StrategyActivity.class);
                break;
            case R.id.ivGame5:
                StrategyActivity.type=4;
                startActivity(StrategyActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean=homeStrategyAdapter.getItem(position);
        BaseWebviewActivity.open(mContext,bean.getUrl(),bean.getName());
    }

    @Override
    protected void initData() {
        getData();
        setData(0);
    }

    private void getData() {
        StkResApi.getTagResourceList(null, AppConfig.HOME_DATA, StkApi.createParamMap(0, 8),false,
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            if (result != null && result.size() > 0) {
                                homeStrategyAdapter.setList(result);
                            }
                        }
                    }
                });
    }

    private void setData(int position){
        mDataBinding.tvGameName.setText(listBanner.get(position).getName());
        mDataBinding.tvGameDate.setText(listBanner.get(position).getDate());
    }

}